package com.ds.esd.custom.properties;

import com.ds.common.util.StringUtility;
import com.ds.esd.annotation.ui.BorderType;
import com.ds.esd.bean.TreeListItem;
import com.ds.esd.bean.view.NavMenuBarViewBean;
import com.ds.esd.dsm.view.field.ESDFieldConfig;
import com.ds.esd.dsm.view.field.FieldModuleConfig;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class NavMenuListItem<T extends TreeListItem> extends TreeListItem {
    public NavMenuListItem() {
        super();
    }


    public NavMenuListItem(NavMenuBarViewBean viewBean) {
        this.id = viewBean.getId();
        if (viewBean.getIndex() != null && viewBean.getIndex() != -1) {
            this.tabindex = viewBean.getIndex();
        }
        this.caption = viewBean.getCaption();
        this.setBorderType(BorderType.none);
        this.imageClass = viewBean.getImageClass();
        this.tagVar = viewBean.getTagVar();
        if (tagVar == null) {
            this.tagVar = new HashMap<>();
        }
        if (viewBean.getNavItems().size() > 0) {
            sub = new ArrayList<>();
            this.setIniFold(false);
            List<FieldModuleConfig> fieldConfigs = viewBean.getNavItems();
            for (ESDFieldConfig childItemInfo : fieldConfigs) {
                FieldModuleConfig child = (FieldModuleConfig) childItemInfo;
                sub.add((T) new NavMenuListItem(child));
            }
        }

    }


    public NavMenuListItem(FieldModuleConfig itemConfig) {
        this.caption = itemConfig.getCaption();
        this.imageClass = itemConfig.getImageClass();
        this.euClassName = itemConfig.getEuClassName();
        this.tabindex = itemConfig.getIndex();
        this.index = itemConfig.getIndex();
        if (euClassName == null) {
            String url = itemConfig.getUrl();
            if (url.startsWith("/")) {
                url = url.substring(1);
            }
            euClassName = StringUtility.replace(url, "/", ".");
        }
        this.id = itemConfig.getId();
        this.setIniFold(false);
        if (tagVar == null) {
            this.tagVar = new HashMap<>();
        }

    }
}
